package blazingbubble.graphics;

import blazingbubble.config.Config;
import blazingbubble.exception.StartupException;
import blazingbubble.game.ILobbyGameEventListener;
import blazingbubble.graphics.lobby.IGameListUpdateListener;
import blazingbubble.graphics.lobby.Lobby;
import blazingbubble.graphics.game.Game;
import blazingbubble.graphics.game.Scene;
import blazingbubble.network.game.strategy.IJoinGameResponseResultListener;
import blazingbubble.network.game.strategy.IStartGameListener;
import blazingbubble.network.lobby.strategy.ILoginResponseResultListener;
import blazingbubble.game.command.GameCommandFactory;
import blazingbubble.lobby.command.LobbyCommandFactory;

import javax.swing.*;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCanvas;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.apache.log4j.Logger;

public class GameWindow extends JFrame implements IJoinGameResponseResultListener, ILoginResponseResultListener, IStartGameListener {
	private static Logger logger = Logger.getLogger(GameWindow.class);

	private blazingbubble.graphics.lobby.Lobby lobbyPanel;
	private blazingbubble.graphics.game.Game gamePanel;

	private GLCanvas canvas;

	public GameWindow(
			GameCommandFactory gameCommandFactory, LobbyCommandFactory lobbyCommandFactory,
			blazingbubble.game.Game game, ILobbyGameEventListener lobbyGameEventListener, Integer frameBreak) {
		GLCapabilities glcaps = new GLCapabilities();
		try {
		canvas = new GLCanvas(glcaps);
		}
		catch(Throwable e) {
			e.printStackTrace();
		}
		getContentPane().add(canvas);

		lobbyPanel = new Lobby(getContentPane(), lobbyCommandFactory, gameCommandFactory, lobbyGameEventListener, this);
		gamePanel = new Game(getContentPane(), gameCommandFactory, game, frameBreak);

		lobbyPanel.getLobbyScreen().setJoinGameListener(gamePanel.getGameStartup());

		lobbyPanel.activate();
	}

	public Game getGamePanel() {
		return gamePanel;
	}

	public void startup(Config config) throws StartupException {
		setTitle("Blazing Bubble");
		setSize(new Dimension(800, 600));

		gamePanel.startup(config);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				shutdown();
				dispose();
			}
		}

		);
	}

	public void shutdown() {
	}

	public void update() {
		canvas.display();
	}

	@Override
	public void joinSuccessful() {
		gamePanel.activate();
	}

	@Override
	public void joinFailed(String reason) {
		//TODO : auto generated stub
		throw new RuntimeException("Not implemented");
	}

	@Override
	public void loginSuccessfull() {
		lobbyPanel.loginPerformed();
	}

	@Override
	public void loginFailed(String reason) {
		//TODO : auto generated stub
		throw new RuntimeException("Not implemented");
	}

	@Override
	public void gameStarted() {
		gamePanel.gameStarted();
	}

	public IGameListUpdateListener getGameListUpdateListener(){
		return lobbyPanel.getGameListUpdateListener();
	}
}
